package com.guli.edu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.common.result.R;
import com.guli.edu.entity.EduCourse;
import com.guli.edu.entity.dto.ChapterDto;
import com.guli.edu.entity.dto.CoursePublishDto;
import com.guli.edu.entity.form.CourseForm;
import com.guli.edu.entity.form.CourseInfoForm;
import com.guli.edu.service.EduChapterService;
import com.guli.edu.service.EduCourseService;
import com.guli.edu.vo.CourseWebVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Daniel
 * @since 2019-03-26
 */
@Api(description = "课程管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private EduChapterService eduChapterService ;

    @ApiOperation(value="前台课程分页列表")
    @GetMapping("getListPage/{page}/{limit}")
    public  R getListPage(@PathVariable Long page,@PathVariable Long limit  ){
        Map<String, Object > map = eduCourseService.getPageList(page,limit);
        return R.ok().data(map);
    }

    @ApiOperation(value="查询所有的课程")
    @GetMapping("{page}/{limit}")
    public R pageQuery(  @ApiParam(name="page" ,value="当前页码" ,required =  false)@PathVariable Long page ,
                         @ApiParam(name="limit" , value="每页记录数",required = false )@PathVariable Long limit,
                         @ApiParam(name = "courseForm", value = "查询对象", required = false)  CourseForm courseForm ){
        Page<EduCourse> pageParam = new Page<>(page,limit);
        eduCourseService.pageQuery(pageParam,courseForm);
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
       return  R.ok().data("total",total).data("rows",records);
    }


    @ApiOperation(value="发布课程")
    @PutMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id ){
        eduCourseService.publishCourse(id);
        return R.ok();
    }


    @ApiOperation(value="根据课程id查询课程详情信息")
    @GetMapping("getCourseSubTeacherInfo/{id}")
    public R getCourseSubTeacherInfo(@PathVariable String id ){
        CoursePublishDto courseInfo = eduCourseService.getCourseInfoId(id);
        return R.ok().data("item",courseInfo);
    }



    @ApiOperation(value = "根据id更新课程")
    @PostMapping("updateCourse/{id}")
    public  R updateCourseById(@PathVariable String id ,@RequestBody CourseInfoForm courseInfoForm){
        boolean  flag = eduCourseService.updateCourseById(courseInfoForm);
        if(flag){
            return  R.ok() ;
        }else{
           return  R.error();
        }
    }

    @ApiOperation(value = "根据课程id查询信息")
    @GetMapping("{id}")
    public R getCourseInfoId(@PathVariable String id ){
        CourseInfoForm courseInfoForm =  eduCourseService.getCourseById(id);
        return R.ok().data("courseInfo",courseInfoForm);
    }

    @ApiOperation(value = "获取课程基本信息")
    @PostMapping("info")
    public R saveCourseInfo(@ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
                            @RequestBody CourseInfoForm courseInfoForm) {
        String courseId = eduCourseService.saveCourseInfo(courseInfoForm);
        if(!StringUtils.isEmpty(courseId)){
            return R.ok().data("courseId",courseId);
        }else{
            return R.error().message("保存失败");
        }
    }

    @ApiOperation("根据id删除课程")
    @DeleteMapping("{courseId}")
    public R removeVideoList(@PathVariable  String courseId ) {

        boolean flag = eduCourseService.removeCourseById(courseId);
        if (flag) {
            return R.ok();
        }else{
            return  R.error();
        }

    }

    @ApiOperation(value="根据课程查询ID")
    @GetMapping(value = "getByCourseId/{courseId}")
    public R getById( @ApiParam(name = "courseId", value = "课程ID", required = true)
                          @PathVariable String courseId){
        //查询课程信息和讲师信息
        CourseWebVo courseWebVo = eduCourseService.selectInfoWebById(courseId);

        List<ChapterDto> chapterVoList = eduChapterService.getAllChapterVideo(courseId);
        return R.ok().data("course",courseWebVo).data("chapterVoList",chapterVoList);
    }

}

